package com.jms.fu.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.App;
import com.jms.fu.model.bo.AppBO;

public class getAppDetailJSONAction extends Action {
	private static Logger logger = Logger.getLogger(LoginAction.class);
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			PrintWriter out = response.getWriter();
			int appid = Integer.parseInt(request.getParameter("appid"));
			AppBO appbo = new AppBO();
			App app = appbo.getAppByID(appid);
			String result = "{\"name\":\"" + app.getAppName() + "\","
					+ "\"price\":\"" + app.getPrice() + "\","
					+ "\"description\":\"" + app.getDescription() + "\","
					+ "\"requireos\":\"" + app.getRequireOS() + "\","
					+ "\"version\":\"" + app.getUpVersion() + "\"}";
			out.print(result);
			out.close();
		} catch (NumberFormatException | IOException | DAOException e) {
			logger.error(e.getMessage());
			PrintWriter out;
			try {
				out = response.getWriter();
				out.print("error");
				out.close();
			} catch (IOException e1) {
				logger.error(e1.getMessage());
			}
		}
		return null;
	}

}
